//
//  WSNewFeatureController.swift
//  微博
//
//  Created by 武莎莎 on 15/9/6.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

private let reuseID = "newFeatureCell"
private let WSNewFeatureCount  = 4

class WSNewFeatureController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 2.注册可重用cell
        // WSNewFeatureCell.self ：类似于OC中的[WSNewFeatureCell class]
        self.collectionView?.registerClass(WSNewFeatureCell.self, forCellWithReuseIdentifier: reuseID)
        
        prepareLayout()
    }

    // 1.设置flowlayout
    init(){
        
        super.init(collectionViewLayout: UICollectionViewFlowLayout())
    }
   
    private func prepareLayout(){
        
        let layout =  collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = view.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return WSNewFeatureCount
    }
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseID, forIndexPath: indexPath) as! WSNewFeatureCell
        
//        cell.backgroundColor = UIColor.greenColor()
    
        cell.imageIndex = indexPath.item
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        // 取得当前显示的cell的indexPath
        let currentIndexPath = collectionView.indexPathsForVisibleItems().last!
        
        if currentIndexPath.item == WSNewFeatureCount - 1 {
            let cell = collectionView.cellForItemAtIndexPath(currentIndexPath) as! WSNewFeatureCell
            cell.showStartBtn()
        }
    }
}
// 私有类，只能在这里使用
private class WSNewFeatureCell:UICollectionViewCell{

    // 图片索引
    private var imageIndex:Int = 0 {
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startBtn.hidden = true
        }
    }
    // 类是private，即使方法名前面没有private，运行循环同样无法调用方法，在方法名前面加上@objc就可以被运行循环调用了
    @objc func clickedStartBtn(){
    
        NSNotificationCenter.defaultCenter().postNotificationName(WSSwitchRootViewControllerNotification, object: nil)
    }
    // MARK:动画显示启动按钮
    private func showStartBtn(){
        startBtn.hidden = false
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        // usingSpringWithDamping ：弹性系数：0~1，越小越弹
        // Velocity : 初始速度
        UIView.animateWithDuration(1.2, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
                // 复原
                self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI(){
        
        addSubview(iconView)
        addSubview(startBtn)
        
        iconView.frame = bounds

        startBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160))
    }
    
    //懒加载cell属性
    private lazy var iconView = UIImageView()
    private lazy var startBtn:UIButton = {
        let btn = UIButton()
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.sizeToFit()
        
        btn.addTarget(self, action: "clickedStartBtn", forControlEvents: UIControlEvents.TouchUpInside)

        return btn
    }()
}
